<?php
require_once 'MDB2.php';
require_once 'config.function.php';
/**
 *function connect_db 
 *
 *从配置文件中读取数据库信息
 *并据此建立一个PEAR MDB2的
 *对象实例，并返回之。
 *
 *@return MDB2 $db
 */
function db_connect(){
	/**
	 *调用get_config()函数读取配置信息
	 *并返回到$config数组中。
	 */

	//@todo 判断配置文件是否存在
	$setting = get_setting();
	$database = $setting->getItem('section','database')->toArray();

	$dbms_type = $database['database']['dbms_type'] ;
	$user = $database['database']['user'];
	$passwd = $database['database']['passwd'] ;
	$host = $database['database']['host'];
	$db_name = $database['database']['db_name'];
	
	//@todo 加到配置选项中？
	$options = array('debug' => 2,'result_buffering' => true,);

	$dsn = "$dbms_type://$user:$passwd@$host/$db_name";

	$db =& MDB2::singleton($dsn, $options);

	if (PEAR::isError($db)) {

    		throw new Exception ($db->getMessage());
	}else{

		return $db ;
	}
}
db_connect();
?>
